package com.dada.controller;

import com.alibaba.fastjson.JSON;
import com.dada.domain.User;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by jianweilin on 16/8/24.
 */
@Controller
@RequestMapping(value = "/hello1/api")
@Api(value = "/hello1",description = "用户相关接口")
public class HelloController1 {

    private static final Logger logger = LoggerFactory.getLogger(HelloController1.class);
    @ResponseBody
    @RequestMapping(value = "/put", method = RequestMethod.POST)
    @ApiOperation(value = "推送数据", notes = "将BI数据推送到服务端")
    @ApiResponses(value = {@ApiResponse(code = 400,message = "client error"),@ApiResponse(code = 500,message="server error")})
    public String push(@ApiParam(value = "用户名",required = true)@RequestParam String username) {
        logger.info("push date server");
        return "push data to server";
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/text")
    @ApiOperation(value = "登录", notes = "用户登录网站")
    public String login(@ApiParam(required = true, name = "use", value = "用户对象") @RequestBody User user,@ApiParam(value = "用户名")@RequestHeader("name") String username) {
        return "login";
    }

    @ResponseBody
    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取信息", notes = "获取用户信息")
    public String list(@ApiParam(required = true, name = "username", value = "姓名") @RequestParam String username,
                       @ApiParam(required = true, name = "password", value = "密码") @RequestParam String password,
                       @ApiParam(required = true, name = "address", value = "地址") @RequestParam String address) {
        return username + " : " + password + " : " + address;
    }

    @ResponseBody
    @RequestMapping(value = "/user/submit",method = RequestMethod.GET)
    @ApiResponses(value = @ApiResponse(code = 200,response = User.class,message = "成功"))
    public String submit(@RequestParam String name,String password){
        logger.info("name ===> " + name);
        return JSON.toJSONString(new User(name,password,"上海"));
    }

    @ResponseBody
    @RequestMapping(value = "/user/info")
    public String info(String name ,String password,@RequestParam String info){
        return name + ":" + password + ":" + info;
    }

    @ResponseBody
    @RequestMapping(value = "/user/test1",method = RequestMethod.POST,consumes = "application/json")
    public String test1(@RequestBody User user){
        return "user : " + user.getUsername() ;
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView model = new ModelAndView();
        model.setViewName("index");
        model.addObject("key","value");
        return model;
    }
}
